package bc.view; 

import bc.event.CardClick;

import bc.model.Card;

import bpmjs.ContextBuilder;
import bpmjs.Event;
import bpmjs.MouseEvent;

import js.Dom;
import js.Lib;
import haxe.rtti.Infos;
import haxe.Timer;

using bpmjs.MooToolsAdapter;

@ManagedEvents("cardClick")
class CardPresenter1 extends CardPresenter, implements Infos
{
	private var cardDiv : HtmlDom;
	
	public function new()
	{
		super();
	}
	
	override public function createElement(card : Card) : HtmlDom
	{
		super.createElement(card);
		
		cardDiv = Lib.document.createDiv();
		
		var image = Lib.document.createImage();
		image.width = 300;
		image.height = 200;
		image.src = "image/bw.jpg";
		image.addEventListener(MouseEvent.CLICK, handleMouseClick);

		cardDiv.appendChild(image);
		
		var div2 = Lib.document.createDiv();
		div2.innerHTML = card.name;
		div2.className = "cardText";
		cardDiv.appendChild(div2);
		
		var timer = new Timer(Std.int(1000 / 60));
		timer.run = handleTimer;
		
		update();
		
		return cardDiv;
	}
	
	private function update()
	{
		cardDiv.setStyle("opacity", cast card.moveSetAlpha.current);
		cardDiv.setStyle("left", cast card.moveSetX.current);
		cardDiv.setStyle("top", cast card.moveSetY.current);
	}
	
	private function handleTimer()
	{
		update();
	}
	
	private function handleMouseClick(event : Event)
	{
		dispatchEvent(new CardClick(card));
	}
}